package com.ruoyi.factorydata.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.factorydata.domain.FactoryData;
import com.ruoyi.factorydata.domain.dto.WorkShopDataDTO;
import com.ruoyi.factorydata.domain.vo.WorkshopDataVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.factorydata.domain.WorkshopData;
import com.ruoyi.factorydata.service.IWorkshopDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车间数据Controller
 * 
 * @author ruoyi
 * @date 2025-06-23
 */
@RestController
@RequestMapping("/workshopdata/workshopdata")
public class WorkshopDataController extends BaseController
{
    @Autowired
    private IWorkshopDataService workshopDataService;

    /**
     * 查询车间数据列表
     */
    @PreAuthorize("@ss.hasPermi('workshopdata:workshopdata:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody WorkShopDataDTO workShopDataDTO)
    {
        startPage();
        List<WorkshopDataVo> list = workshopDataService.selectWorkshopDataList(workShopDataDTO);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(new PageInfo(list).getTotal());
        return tableDataInfo;
    }


    @GetMapping("/selectWorkShopByKeyword")
    public AjaxResult selectWorkShopByKeyword(@RequestParam(required = false) String keyword,
                                              @RequestParam(required = false) Long factoryId) {
        startPage();
        List<WorkshopData> list = workshopDataService.list(new QueryWrapper<WorkshopData>().lambda()
                .eq(factoryId != null, WorkshopData::getFactoryId, factoryId)
                .like(keyword != null && !keyword.isBlank(), WorkshopData::getName, keyword));
        return AjaxResult.success(list);
    }
    /**
     * 导出车间数据列表
     */
    @PreAuthorize("@ss.hasPermi('workshopdata:workshopdata:export')")
    @Log(title = "车间数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkShopDataDTO workShopDataDTO)
    {
        List<WorkshopDataVo> list = workshopDataService.selectWorkshopDataList(workShopDataDTO);
        ExcelUtil<WorkshopDataVo> util = new ExcelUtil<WorkshopDataVo>(WorkshopDataVo.class);
        util.exportExcel(response, list, "车间数据数据");
    }

    /**
     * 获取车间数据详细信息
     */
//    @PreAuthorize("@ss.hasPermi('workshopdata:workshopdata:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(workshopDataService.selectWorkshopDataById(id));
    }

    /**
     * 新增车间数据
     */
    @PreAuthorize("@ss.hasPermi('workshopdata:workshopdata:add')")
    @Log(title = "车间数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkshopData workshopData)
    {
       boolean exists = workshopDataService.lambdaQuery().eq(WorkshopData::getWorkshopCode, workshopData.getWorkshopCode()).count() > 0;
       if (exists){
           return AjaxResult.warn("车间编码已存在");
       }
        workshopData.setCreateBy(getUserId().toString());
        return toAjax(workshopDataService.insertWorkshopData(workshopData));
    }

    /**
     * 修改车间数据
     */
    @PreAuthorize("@ss.hasPermi('workshopdata:workshopdata:edit')")
    @Log(title = "车间数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkshopData workshopData)
    {
        List<Long> ids = workshopDataService.lambdaQuery()
                .eq(WorkshopData::getWorkshopCode, workshopData.getWorkshopCode())
                .select(WorkshopData::getId).list().stream()
                .map(WorkshopData::getId)
                .collect(Collectors.toList());
        boolean exists = !ids.isEmpty() && !ids.contains(workshopData.getId());
        if (exists){
            return AjaxResult.warn("车间编码已存在");
        }
        return toAjax(workshopDataService.updateWorkshopData(workshopData));
    }

    /**
     * 删除车间数据
     */
    @PreAuthorize("@ss.hasPermi('workshopdata:workshopdata:remove')")
    @Log(title = "车间数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(workshopDataService.deleteWorkshopDataByIds(ids));
    }
}
